﻿

// 项目展示地址:"http://www.ddxc.org/"
// 如果你有什么好的建议或者觉得可以加什么功能，请加QQ群：104390185大家交流沟通
// 项目展示地址:"http://www.yoyocms.com/"
//博客地址：http://www.cnblogs.com/wer-ltm/
//代码生成器帮助文档：http://www.cnblogs.com/wer-ltm/p/5777190.html
// <Author-作者>角落的白板笔</Author-作者>
// Copyright © YoYoCms@中国.2017-02-04T15:49:20. All Rights Reserved.
//<生成时间>2017-02-04T15:49:20</生成时间>
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using ShiTong.PhoneBook.Persons.Authorization;
using ShiTong.PhoneBook.Persons.Dtos;

namespace ShiTong.PhoneBook.Persons
{
    /// <summary>
    /// 联系人服务实现
    /// </summary>
    [AbpAuthorize(PersonAppPermissions.Person)]
    public class PersonAppService : PhoneBookAppServiceBase, IPersonAppService
    {
        private readonly IRepository<Person, long> _personRepository;
        private readonly PersonManage _personManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public PersonAppService(IRepository<Person, long> personRepository, PersonManage personManage)
        {
            _personRepository = personRepository;
            _personManage = personManage;
        }

        #region 联系人管理

        /// <summary>
        /// 根据查询条件获取联系人分页列表
        /// </summary>
        public async Task<PagedResultDto<PersonListDto>> GetPagedPersonsAsync(GetPersonInput input)
        {

            var query = _personRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var personCount = await query.CountAsync();

            var persons = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var personListDtos = persons.MapTo<List<PersonListDto>>();
            return new PagedResultDto<PersonListDto>(
            personCount,
            personListDtos
            );
        }

        /// <summary>
        /// 通过Id获取联系人信息进行编辑或修改 
        /// </summary>
        public async Task<GetPersonForEditOutput> GetPersonForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetPersonForEditOutput();

            PersonEditDto personEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _personRepository.GetAsync(input.Id.Value);
                personEditDto = entity.MapTo<PersonEditDto>();
            }
            else
            {
                personEditDto = new PersonEditDto();
            }

            output.Person = personEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取联系人ListDto信息
        /// </summary>
        public async Task<PersonListDto> GetPersonByIdAsync(EntityDto<long> input)
        {
            var entity = await _personRepository.GetAsync(input.Id);

            return entity.MapTo<PersonListDto>();
        }







        /// <summary>
        /// 新增或更改联系人
        /// </summary>
        public async Task CreateOrUpdatePersonAsync(CreateOrUpdatePersonInput input)
        {
            if (input.PersonEditDto.Id.HasValue)
            {
                await UpdatePersonAsync(input.PersonEditDto);
            }
            else
            {
                await CreatePersonAsync(input.PersonEditDto);
            }
        }

        /// <summary>
        /// 新增联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_CreatePerson)]
        public virtual async Task<PersonEditDto> CreatePersonAsync(PersonEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<Person>();

            entity = await _personRepository.InsertAsync(entity);
            return entity.MapTo<PersonEditDto>();
        }

        /// <summary>
        /// 编辑联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_EditPerson)]
        public virtual async Task UpdatePersonAsync(PersonEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _personRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _personRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_DeletePerson)]
        public async Task DeletePersonAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _personRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_DeletePerson)]
        public async Task BatchDeletePersonAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _personRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        public Task<PersonListDto> GetPersonToExcel()
        {
            throw new NotImplementedException();
        }


        #endregion










    }
}
